package com.melo;

import lombok.Data;

import java.util.Arrays;

/**
 * @author lichunlong lcl253137@alibaba-inc.com
 * @desc 弗洛伊德算法: 村庄实体类
 * @date 2025-06-12
 */
@Data
public class FloydVillageGraph {

    /**
     * 存放村庄顶点的数组
     */
    private char[] villageVertexArr;

    /**
     * 从各个顶点出发到其它顶点的距离
     */
    private int[][] distanceMatrixArr;

    /**
     * 到达各个目标顶点的前驱顶点
     */
    private int[][] preVillageVertexMatrixArr;

    public FloydVillageGraph(char[] villageVertexArr, int[][] distanceMatrixArr) {
        this.villageVertexArr = villageVertexArr;
        this.distanceMatrixArr = distanceMatrixArr;
        this.preVillageVertexMatrixArr = new int[villageVertexArr.length][villageVertexArr.length];
        // 对pre数组初始化, 注意存放的是前驱顶点的下标, 初始化为自身元素的下标
        for (int i = 0; i < villageVertexArr.length; i++) {
            Arrays.fill(preVillageVertexMatrixArr[i], i);
        }
    }

    // 显示pre数组和dis数组
    public void showMatrix() {
        // 为了显示便于阅读, 优化一下输出
        for (int k = 0; k < distanceMatrixArr.length; k++) {
            // 先输出一行pre数组
            for (int i = 0; i < distanceMatrixArr.length; i++) {
                System.out.print(villageVertexArr[preVillageVertexMatrixArr[k][i]] + "\t");
            }
            System.out.println();
            // 再输出一行dis数组
            for (int i = 0; i < distanceMatrixArr.length; i++) {
                System.out.print("(" + villageVertexArr[k] + "到" + villageVertexArr[i] + "的最短路径是" + distanceMatrixArr[k][i] + ")\t");
            }
            System.out.println();
        }
    }
}
